<template>
  <basic-container>
    <!-- 提示 -->
    <h4>提示：如需查看完整手机号，请联系客户经理，缴纳数据保证金</h4>
    <avue-crud
      :option="option"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="myplace" slot-scope="{row}">
        <el-tag>{{GetDistance(row.tx, row.ty, row.x, row.y)}}</el-tag>
      </template>

      <template slot="place" slot-scope="{row}">
        <el-button type="success" plain size="small" @click="placeShow(row)">位置</el-button>
      </template>
      <template slot="ask" slot-scope="{row}">
        <el-button @click="msgBtn(row)" type="primary" plain size="small">消息</el-button>
      </template>
    </avue-crud>

    <!-- 聊天框 -->
    <el-dialog :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <chat :data="row"></chat>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible1" width="50%" :before-close="handleClose1">
      <baidu-map class="map" :center="center" :zoom="15" :scroll-wheel-zoom="true"></baidu-map>
    </el-dialog>
  </basic-container>
</template>
<script>
import { getList, remove, update, add, getNotice } from "@/api/custom/ask";
import { mapGetters } from "vuex";
import chat from "@/components/chat";

export default {
  components: {
    chat
  },
  data() {
    return {
      center: {
        lng: 23.16,
        lat: 53.12
      },
      dialogVisible: false,
      dialogVisible1: false,
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        addBtn: false,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        columnBtn: false,
        refreshBtn: false,
        searchBtn: false,
        editBtn: false,
        column: [
          {
            label: "微信昵称",
            prop: "wechatName",
            row: true,
            search: false
          },
          {
            label: "手机号",
            prop: "telephone",
            row: true,
            search: false
          },
          {
            label: "地理位置",
            prop: "place",
            row: true,
            search: false,
            slot: true
          },

          {
            label: "距离（KM）",
            prop: "myplace",
            row: true,
            slot: true,
            search: false
          },
          {
            label: "最近咨询时间",
            prop: "consultTime",
            row: true,
            search: false
          },
          {
            label: "会话",
            prop: "ask",
            row: true,
            slot: true,
            search: false
          }
        ]
      },
      data: [],
      lang: 0,
      row: ""
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.notice_add, false),
        viewBtn: this.vaildData(this.permission.notice_view, false),
        delBtn: this.vaildData(this.permission.notice_delete, false),
        editBtn: this.vaildData(this.permission.notice_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    placeShow(row) {
      this.dialogVisible1 = true;
      // this.center.lng = row.x
      // this.center.lat = row.y
      console.log(row);
    },
    closeDialog() {},
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
          this.$router.go(0);
        })
        .catch(() => {});
    },
    handleClose1(done) {
      done();
    },
    // 消息按钮
    msgBtn(row) {
      this.dialogVisible = true;
      this.row = row;
    },
    GetDistance(lat1, lng1, lat2, lng2) {
      var radLat1 = (lat1 * Math.PI) / 180.0;
      var radLat2 = (lat2 * Math.PI) / 180.0;
      var a = radLat1 - radLat2;
      var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
      var s =
        2 *
        Math.asin(
          Math.sqrt(
            Math.pow(Math.sin(a / 2), 2) +
              Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)
          )
        );
      s = s * 6378.137; // EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000;
      return parseInt(s);
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      const { releaseTimeRange } = params;
      let values = {
        ...params
      };
      if (releaseTimeRange) {
        values = {
          ...params,
          releaseTime_gt: releaseTimeRange[0],
          releaseTime_lt: releaseTimeRange[1]
        };
        values.releaseTimeRange = null;
      }
      getList(page.currentPage, page.pageSize, values).then(res => {
        const data = res.data.data;
        for (var k in data) {
          var docList = data[k];
          this.data = docList.records;
          console.log(this.data);
        }
        this.page.total = docList.total;
        this.data.forEach(item => {});
      });
    }
  }
};
</script>
<style>
h4 {
  color: red;
}

.map {
  width: 800px;
  height: 400px;
}
</style>
